package com.luca.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luca.common.utils.StringUtils;
import com.luca.system.domain.MfcBriefs;
import com.luca.system.mapper.MfcBriefsMapper;
import com.luca.system.service.IMfcBriefsService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 蜜蜂查概要Service业务层处理
 *
 * @author luca
 * @date 2021-04-09
 */
@Service
public class MfcBriefsServiceImpl extends ServiceImpl<MfcBriefsMapper, MfcBriefs> implements IMfcBriefsService {

    /**
     * 查询蜜蜂查概要
     *
     * @param id 蜜蜂查概要ID
     * @return 蜜蜂查概要
     */
    @Override
    public MfcBriefs selectMfcBriefsById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询蜜蜂查概要列表
     *
     * @param mfcBriefs 蜜蜂查概要
     * @return 蜜蜂查概要
     */
    @Override
    public List<MfcBriefs> selectMfcBriefsList(MfcBriefs mfcBriefs) {
        var query = new QueryWrapper<MfcBriefs>();
        if (StringUtils.isNotBlank(mfcBriefs.getTitle())) {
            query.like("title", mfcBriefs.getTitle());
        }
        query.orderByDesc("id");
        return baseMapper.selectList(query);
    }

    /**
     * 新增蜜蜂查概要
     *
     * @param mfcBriefs 蜜蜂查概要
     * @return 结果
     */
    @Override
    public int insertMfcBriefs(MfcBriefs mfcBriefs) {
        return baseMapper.insert(mfcBriefs);
    }

    /**
     * 修改蜜蜂查概要
     *
     * @param mfcBriefs 蜜蜂查概要
     * @return 结果
     */
    @Override
    public int updateMfcBriefs(MfcBriefs mfcBriefs) {
        return baseMapper.updateById(mfcBriefs);
    }

    /**
     * 批量删除蜜蜂查概要
     *
     * @param ids 需要删除的蜜蜂查概要ID
     * @return 结果
     */
    @Override
    public int deleteMfcBriefsByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除蜜蜂查概要信息
     *
     * @param id 蜜蜂查概要ID
     * @return 结果
     */
    @Override
    public int deleteMfcBriefsById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public void pull(List<Map<String, Object>> data) {
        var saveList = new ArrayList<MfcBriefs>();
        data.forEach(one -> {
            var count = baseMapper.selectCount(new QueryWrapper<MfcBriefs>().eq("timestamp", one.get("timestamp")));
            if (count == 0) {
                var briefs = new MfcBriefs();
                briefs.setTitle((String) one.get("title"));
                briefs.setContent((String) one.get("content"));
                briefs.setTimestamp((Long) one.get("timestamp"));
                briefs.setUrl((String) one.get("url"));
                briefs.setSource((String) one.get("source"));
                saveList.add(briefs);
            }
        });
        if (!CollectionUtils.isEmpty(saveList)) {
            saveBatch(saveList);
        }
    }

}
